import {EventEmitter, Injectable} from '@angular/core';
import {Http, RequestOptions} from "@angular/http";
import {Maintenance} from "../model/maintenance";
import MTSConstants from "../config/config";
import {BackCode} from "../model/backCode";

@Injectable()
export class MaintenanceService {
  servicename:string;
  searchEvent: EventEmitter<string> = new EventEmitter(); //查询关键字
  listFreshEvent: EventEmitter<string> = new EventEmitter();  //刷新列表项
  searchFreshEvent: EventEmitter<string> = new EventEmitter();  //清空搜索框
  constructor(private http:Http) { }

  //获取维护类型信息列表
  getMaintenanceTypes(): Promise<Maintenance[]> {
    // return this.http.get(`/api/room`).toPromise().then(res =>res.json()).catch(this.handleError);
    const url = `${MTSConstants.HOSTUser}maintain/types`;
    return this.http.get(url)
      .toPromise()
      .then((res) => { return res.json() as Maintenance[]; })
      .catch((error: any) => {return this.handleError('getMaintenanceTypes',error);});
  }

  //根据OID查询维护类型信息
  getMaintenanceTypeByOid(oid:string): Promise<Maintenance>{
    const url = `${MTSConstants.HOSTUser}maintain/type/${oid}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => {return res.json() as Maintenance;})
      .catch((error: any) => {return this.handleError('getMaintenanceTypeByOid',error);});
  }

  //新建维护类型信息
  createMaintenanceType(postvalue:Maintenance): Promise<BackCode> {
    const url = `${MTSConstants.HOSTUser}maintain/type`;
    const body = JSON.stringify(postvalue);//这个也可以
    //const body = postvalue;//这个可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.post(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('createMaintenanceType',error);});
  }

  //更新维护类型信息
  updateMaintenanceType(postvalue: Maintenance): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}maintain/type`;
    //let body = JSON.stringify(val);//这个也可以
    // postvalue.oid = "ce8750aba4994ea59d94b22cae64ca0f";
    const body = postvalue;//这个可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.put(url, body, options)
      .toPromise()
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('updateMaintenanceType',error);});
  }

  //删除维护类型信息
  deleteMaintenanceType(oid:string): Promise<BackCode>{
    const url = `${MTSConstants.HOSTUser}maintain/type/${oid}`;
    //let body = JSON.stringify(val);//这个也可以
    const headers = MTSConstants.headers;//spring的restful接口用这个
    const options = new RequestOptions({ headers: headers });
    return this.http.delete(url, options)
      .toPromise()
      //.then((res) =>{console.log(res)})
      .then((res) => { return res.json() as BackCode;})
      .catch((error: any) => {return this.handleError('deleteMaintenanceType',error);});
  }

  //检查维护类型ID是否存在
  checkMaintenanceTypeId(id:string): Promise<BackCode>{
    // id = "NJM302";
    const url = `${MTSConstants.HOSTUser}maintain/typeid/${id}`;
    return this.http.get(url)
      .toPromise()
      .then((res) => {return res.json() as BackCode;})
      //1:存在，0:不存在
      .catch((error: any) => {return this.handleError('checkMaintenanceTypeId',error);});
  }

  handleError(functionName:string,error: any): Promise<any> {
    console.error(`服务 : ${this.servicename} , 函数 : ${functionName} 。发生错误 : `, error);
    return Promise.reject(error.message || error);
  }
}
